package com.ck567.syn;

import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * @author wuhao
 * @date 2021/1/13 13:40
 */
public class SynThread implements Runnable {

    public void run() {
        String threadName = Thread.currentThread().getName();
        if(threadName.startsWith("A")){
            async();
        }else if (threadName.startsWith("B")){
            syncObjectBlock1();
        }else  if(threadName.startsWith("C")){
            syncObjectMethod1();
        }else if (threadName.startsWith("D")){
            syncClassBlock1();
        }else  if(threadName.startsWith("E")){
            syncClassMethod1();
        }
    }
    //异步
    private void async(){
        try {
            System.out.println(Thread.currentThread().getName()+"_Async_Start:"+new SimpleDateFormat("HH-mm-ss").format(new Date()));
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName()+"_Async_End:"+new SimpleDateFormat("HH-mm-ss").format(new Date()));

        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }

    /**
     * 方法中有synchronized同步代码块 对象锁
     */
    private void syncObjectBlock1(){
        synchronized (this){
            System.out.println(Thread.currentThread().getName()+"_SyncObjectBlock1:"+new SimpleDateFormat("HH-mm-ss").format(new Date()));
            try {
                System.out.println(Thread.currentThread().getName()+"_SyncObjectBlock1_Start:"+new SimpleDateFormat("HH-mm-ss").format(new Date()));
                Thread.sleep(1000);
                System.out.println(Thread.currentThread().getName()+"_SyncObjectBlock1_End:"+new SimpleDateFormat("HH-mm-ss").format(new Date()));

            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }

    }

    /**
     * synchronized修饰非静态方法  对象锁
     */
    private synchronized void syncObjectMethod1(){
        System.out.println(Thread.currentThread().getName()+"_SyncObjectBlock1:"+new SimpleDateFormat("HH-mm-ss").format(new Date()));
        try {
            System.out.println(Thread.currentThread().getName()+"_SyncObjectMethod1_Start:"+new SimpleDateFormat("HH-mm-ss").format(new Date()));
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName()+"_SyncObjectMethod1_End:"+new SimpleDateFormat("HH-mm-ss").format(new Date()));

        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }



    /**
     * 方法中有synchronized同步代码块 类锁
     */
    private void syncClassBlock1(){
        synchronized (SynThread.class){
            System.out.println(Thread.currentThread().getName()+"_SyncClassBlock1:"+new SimpleDateFormat("HH-mm-ss").format(new Date()));
            try {
                System.out.println(Thread.currentThread().getName()+"_SyncClassBlock1_Start:"+new SimpleDateFormat("HH-mm-ss").format(new Date()));
                Thread.sleep(1000);
                System.out.println(Thread.currentThread().getName()+"_SyncClassBlock1_End:"+new SimpleDateFormat("HH-mm-ss").format(new Date()));

            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }

    }


    /**
     * synchronized修饰静态方法  类锁
     */
    private synchronized static void syncClassMethod1(){
        System.out.println(Thread.currentThread().getName()+"_SyncClassMethod1:"+new SimpleDateFormat("HH-mm-ss").format(new Date()));
        try {
            System.out.println(Thread.currentThread().getName()+"_SyncClassMethod1_Start:"+new SimpleDateFormat("HH-mm-ss").format(new Date()));
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName()+"_SyncClassMethod1_End:"+new SimpleDateFormat("HH-mm-ss").format(new Date()));

        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }
}
